﻿using System.Web.Mvc;
using PTop.Application.WeChat;
using PTop.Front.Controllers;

namespace PTop.Front.Areas.Wechat.Controllers
{
    public class WechatController : PTopControllerBase
    {

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="openid"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetUserDetail(string openid)
        {
            if (string.IsNullOrEmpty(openid))
            {
                return Error("请在微信中访问");
            }
            var userInfoApp = new WxUserInfoApp();
            var wechatAccount = userInfoApp.GetFormByOpenId(openid);
            if (wechatAccount == null)
            {
                new WxApiApp().GetAndSaveUserInfo(openid);
                wechatAccount = userInfoApp.GetFormByOpenId(openid);
            }
            if (wechatAccount != null)
            {
                //var cardBindApp = new CardBindApp();
                //var cardBind = cardBindApp.GetFormByOpenId(openid);
                //var data = new AccountModel
                //{
                //    Name = wechatAccount.F_NickName,
                //    PhotoUrl = wechatAccount.F_HeadImgUrl,
                //    OpenId = wechatAccount.F_OpenId,
                //    IcCardNo = cardBind == null ? null : cardBind.F_CardNo
                //};
                return Success("SUCCESS", null);
            }
            return Error("没有找到该用户");
        }

        public ActionResult BindCard()
        {
            return Success("SUCCESS");
        }

        /// <summary>
        /// 重新获取用户微信信息
        /// </summary>
        /// <param name="openid"></param>
        /// <returns></returns>
        [HandlerAjaxOnly]
        public ActionResult ResetUserDetail(string openid)
        {
            if (string.IsNullOrEmpty(openid))
            {
                return Error("请在微信中访问");
            }
            var dto = new WxApiApp().GetAndSaveUserInfo(openid);
            return Success("获取成功", dto);
        }
    }
}
